<?php

namespace app\api\controller;

use app\admin\model\department\Lists;
use app\admin\model\month\Salary;
use app\admin\model\product\Category;
use app\admin\model\product\Listo;
use app\admin\model\user\Edit;
use app\admin\model\user\Sign;
use app\common\controller\Api;
use app\common\controller\CompanyApi;
use app\common\controller\RedisCluster;
use think\Db;
use think\Exception;

/**
 * 首页接口
 */
class Useredit extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    /**
     * 物资入库
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function create()
    {
        $id = $this->request->post('id');
        $total = $this->request->post('total');
        $remark = $this->request->post('remark');
        $reply_image = $this->request->post('reply_image');
        if (!$id || !$total || !$remark || !$reply_image) {
            $this->error('物资名称，数量，入库说明，签名缺一不可');
        }

        $row = \app\admin\model\user\Lists::find($id);
        if (!$row) {
            $this->error('该物资不存在！');
        }

        Db::startTrans();
        try {

            $user_id = $this->auth->id;
            $data = [
                'lists_id' => $id,
                'before' => $row->stock,
                'after' => $total,
                'user_id' => $user_id,
                'reply_image' => $reply_image,
                'remark' => $remark,
                'status' => 1
            ];

            $ret = Edit::create($data);

            $row->stock = $total;
            $row->save();

            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }

        $this->success('登记成功');
    }

    /**
     * 获取登记详情
     */
    public function read()
    {
        $id = $this->request->post('id');
        if (!$id) {
            $this->error('参数缺失！');
        }
        $row = \app\admin\model\product\Edit::find($id);
        if (!$row) {
            $this->error('该物资盘点登记不存在！');
        }

        $row->reply_image = make_full_url($row->reply_image);

        $row->review_image = $row->review_image ? make_full_url($row->review_image) : '';

        $user = \app\admin\model\User::field('username,nickname')->find();
        $row->user = $user;

        $this->success('登记详情', $row);
    }
}
